//
//  Drop.swift
//  Drop
//
//  Created by zhuxuhong on 2018/3/18.
//  Copyright © 2018年 zhuxuhong. All rights reserved.
//

import UIKit


class Drop: BaseObject {
    var id = 0
    var content = Content()
    var date = Date()
    var image = "cover"
    var likes = 0
    var comments = 0
    var shares = 0
    var views = 0
    var favorites = 0
    var collects = 0
    var isLiked = false
    var isCollected = false
    var author = User()
    
    var isEditing = false
    var isSelected = false
    
    override func setValue(_ value: Any?, forKey key: String) {
        if key == "date", let value = value as? TimeInterval {
            date = .since1970(milliseconds: value)
        }
        else if key == "content",
            let value = value as? JSONDictionary {
            content = .model(with: value)
        }
        else if key == "author",
            let value = value as? JSONDictionary {
            author = .model(with: value)
        }
        else{
            super.setValue(value, forKey: key)
        }
    }
}


extension Drop{
	class Content: BaseObject {
		static func commonAttributes(alignment: Int = 0,
									 lineHeight: CGFloat = 1.5,
									 font: UIFont = Font.p18.boldSystem) -> [NSAttributedString.Key: Any]
		{
			let paragraph = NSMutableParagraphStyle()
			paragraph.lineHeightMultiple = lineHeight
            // 这两项设置，对于文章类Drop才适用
//            paragraph.paragraphSpacing = 2 * font.pointSize
//            paragraph.firstLineHeadIndent = 2 * font.pointSize
			paragraph.alignment = NSTextAlignment(rawValue: alignment) ?? .left
			return [
				NSAttributedString.Key.font: font,
				NSAttributedString.Key.foregroundColor: UIColor.white,
				NSAttributedString.Key.paragraphStyle: paragraph
			]
		}
		
        var byAuthor = "张爱玲"
        var fromBook = "爱"
        var paragraphSpacing = 0
        var firstLineHeadIndent = 0
		var alignment = 0
		var lineHeight: CGFloat = 1.5
		var text = "测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试"
		
        var attributedText: NSAttributedString{
            let content = attributedContent
            let quotes = attributedQuotes
            return NSMutableAttributedString(attrStrings: [
                content,
                quotes
            ])
        }
        
        var attributedQuotes: NSAttributedString{
            let book = fromBook.isEmpty
                ? ""
                : " -《\(fromBook)》"
            return NSAttributedString(string: "\n\n\(byAuthor)\(book)", font: Font.p14.lightSystem, color: .white, alignment: .right)
        }
        
		lazy var attributedContent: NSAttributedString = {
			return NSAttributedString(string: "“\(self.text)”", attributes: Content.commonAttributes(alignment: self.alignment, lineHeight: self.lineHeight))
		}()
	}
}

// MARK: - Sample Data
extension Drop{
    static func sampleData() -> [Drop]{
        var array: [Drop] = []
        do{
            let jsonData = try Data(contentsOf: Bundle.main.url(forResource: "drops.json", withExtension: nil)!)
            guard let json = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as? JSONDictionary else{
                return array
            }
            let data = json["data"] as? [JSONDictionary] ?? []
            array = data.map{ .model(with: $0) }
        }catch{
            fatalError("JSON数据解析失败")
        }
        return array
    }
}

